package com.eqdd.yiqidian.ui.mine.wo

import android.os.Bundle
import android.text.TextUtils
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.AppConstent
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.http.HttpCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.kotlin.KotlinBaseActivity
import com.eqdd.yiqidian.utils.EncryptUtil
import com.eqdd.yiqidian.utils.GreenDaoUtil
import com.eqdd.yiqidian.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_unbinding.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast

/**
 * @author吕志豪 .
 * @date 18-3-26  下午5:09.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

class UnBindingActivity : KotlinBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_unbinding)
        initTopBar("解绑手机")
        tv_old_phone.text = user?.uname

        btn_unbinding.onClick {
            unbinding()
        }


    }

    private fun unbinding() {
        if (TextUtils.isEmpty(tv_password.text)) {
            toast("请输入密码")
            return
        }
        if (TextUtils.isEmpty(tv_new_phone.text)) {
            toast("请输入新手机号")
            return
        }

        val phone = tv_new_phone.text.toString()
        val password = EncryptUtil.sha1(tv_password.text.toString() + "EQD")

        OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.UPDATE_LOGIN_PHONE)
                .params("userGuid", user?.guid)
                .params("password", password)
                .params("uname", phone)
                .execute(object : HttpCallBack<HttpResult<*>>(this@UnBindingActivity) {
                    override fun onSuccess(response: Response<HttpResult<*>>) {

                        val httpResult = response.body()
                        ToastUtil.showShort(httpResult.msg)
                        if (httpResult.status == 200) {
                            val mUser = GreenDaoUtil.getUser()
                            if (mUser != null) {
                                mUser.uname = tv_new_phone.text.toString()
                            }
                            GreenDaoUtil.insert(mUser)
                            AppConstent.user = GreenDaoUtil.getUser()
                            setResult(Config.SUCCESS)
                            finish()
                        }
                    }

                    override fun onError(response: Response<HttpResult<*>>?) {
                        super.onError(response)
                        toast("密码或手机号有误")
                    }
                })


    }


}
